;; **************************************************************************
;; * Symbolics Lisp Machine Keyboard to PS/2 Converter w/ Programmable Keys *
;; *                Copyright (c)2008 Stanislav Datskovskiy                 *
;; *                          stas@loper-os.org                             *
;; *                                                                        *
;; *                    See README for electric schematics                  *
;; *                                                                        *
;; *  This program is free software; you can redistribute it and/or modify  *
;; *  it under the terms of the GNU General Public License as published by  *
;; *  the Free Software Foundation; either version 3 of the License, or     *
;; *  (at your option) any later version.                                   *
;; *                                                                        *
;; *  This program is distributed in the hope that it will be useful,       *
;; *  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
;; *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
;; *  GNU General Public License for more details.                          *
;; *                                                                        *
;; *  You should have received a copy of the GNU General Public License     *
;; *  along with this program.  If not, see <http://www.gnu.org/licenses/>. *
;; **************************************************************************


#include	"common.asm"


	;; ******************************************************************
	;; Constants
	
#define		INIT_DELAY	D'50' ; keyboard reset delay (ms) - each half of wave


	;; ******************************************************************
	;; Pin Actions
	
#define		SYMKBD_CLEAR		bcf	SYM_CLEAR	; clear keyboard
#define		SYMKBD_ENABLE		bsf	SYM_CLEAR	; enable keyboard
#define		SYMKBD_CLOCK_HIGH	bsf	SYM_CLOCK	; raise clock
#define		SYMKBD_CLOCK_LOW	bcf	SYM_CLOCK	; lower clock
#define		IF_SYMKBD_BIT		btfss	SYM_DATA	; skip if no bit

#include	"symkeys.asm"


    extern		SDL
    extern		delay_ms
    extern		Delay_second

    udata

tempvar	res	1

    code


	;; ******************************************************************
	;; Symbolics 3600 Keyboard Routines


	;; Initialize Symbolics Keyboard
sym_kbd_init
	SYMKBD_CLOCK_LOW
	movlw   INIT_DELAY	; set delay length
        movwf   SDL
	SYMKBD_CLEAR
	call	delay_ms
	SYMKBD_ENABLE
	call	delay_ms
	return


sym_kbd_test

scan
	SYMKBD_CLEAR
	SYMKBD_ENABLE
        movlw   D'127'
        movwf   tempvar
scan_inner

	IF_SYMKBD_BIT
	call	test_signal
	
	SYMKBD_CLOCK_HIGH
	SYMKBD_CLOCK_LOW
        decfsz  tempvar, f
        goto    scan_inner

	goto 	scan
	
	return


test_signal
	LED_ON	LED_SCROLL
	call	Delay_second
	LED_OFF	LED_SCROLL
	return


	;; Decode Symbolics Key
decode_sym_key
	addwf	PCL,f
	retlw	NO_KEY		; n=1, no key
	retlw	SKEY_LOCAL	; n=2, LOCAL
	retlw	SKEY_CAPSLOCK	; n=3, CAPSLOCK
	retlw	SKEY_HYPER_L	; n=4, HYPER_L
	retlw	SKEY_META_L	; n=5, META_L
	retlw	SKEY_CONTROL_R	; n=6, CONTROL_R
	retlw	SKEY_SUPER_R	; n=7, SUPER_R
	retlw	SKEY_SCROLL	; n=8, SCROLL
	retlw	SKEY_MODELOCK	; n=9, MODELOCK
	retlw	NO_KEY		; n=10, NO_KEY
	retlw	NO_KEY		; n=11, NO_KEY
	retlw	NO_KEY		; n=12, NO_KEY
	retlw	SKEY_SELECT	; n=13, SELECT
	retlw	SKEY_SYMBOL_L	; n=14, SYMBOL_L
	retlw	SKEY_SUPER_L	; n=15, SUPER_L
	retlw	SKEY_CONTROL_L	; n=16, CONTROL_L
	retlw	SKEY_SPACE	; n=17, SPACE
	retlw	SKEY_META_R	; n=18, META_R
	retlw	SKEY_HYPER_R	; n=19, HYPER_R
	retlw	SKEY_END	; n=20, END
	retlw	NO_KEY		; n=21, no key
	retlw	NO_KEY		; n=22, no key
	retlw	NO_KEY		; n=23, no key
	retlw	SKEY_Z		; n=24, Z
	retlw	SKEY_C		; n=25, C
	retlw	SKEY_B		; n=26, B
	retlw	SKEY_M		; n=27, M
	retlw	SKEY_PERIOD	; n=28, PERIOD
	retlw	SKEY_SHIFT_R	; n=29, SHIFT_R
	retlw	SKEY_REPEAT	; n=30, REPEAT
	retlw	SKEY_ABORT	; n=31, ABORT
	retlw	NO_KEY		; n=32, no key
	retlw	NO_KEY		; n=33, no key
	retlw	NO_KEY		; n=34, no key
	retlw	SKEY_SHIFT_L	; n=35, SHIFT_L
	retlw	SKEY_X		; n=36, X
	retlw	SKEY_V		; n=37, V
	retlw	SKEY_N		; n=38, N
	retlw	SKEY_COMMA	; n=39, COMMA
	retlw	SKEY_SLASH	; n=40, SLASH
	retlw	SKEY_SYMBOL_R	; n=41, SYMBOL_R
	retlw	SKEY_HELP	; n=42, HELP
	retlw	NO_KEY		; n=43, no key
	retlw	NO_KEY		; n=44, no key
	retlw	NO_KEY		; n=45, no key
	retlw	SKEY_RUBOUT	; n=46, RUBOUT
	retlw	SKEY_S		; n=47, S
	retlw	SKEY_F		; n=48, F
	retlw	SKEY_H		; n=49, H
	retlw	SKEY_K		; n=50, K
	retlw	SKEY_SEMICOLON	; n=51, SEMICOLON
	retlw	SKEY_RETURN	; n=52, RETURN
	retlw	SKEY_COMPLETE	; n=53, COMPLETE
	retlw	NO_KEY		; n=54, no key
	retlw	NO_KEY		; n=55, no key
	retlw	NO_KEY		; n=56, no key
	retlw	SKEY_NETWORK	; n=57, NETWORK
	retlw	SKEY_A		; n=58, A
	retlw	SKEY_D		; n=59, D
	retlw	SKEY_G		; n=60, G
	retlw	SKEY_J		; n=61, J
	retlw	SKEY_L		; n=62, L
	retlw	SKEY_APOSTROPHE	; n=63, APOSTROPHE
	retlw	SKEY_LINE	; n=64, LINE
	retlw	NO_KEY		; n=65, no key
	retlw	NO_KEY		; n=66, no key
	retlw	NO_KEY		; n=67, no key
	retlw	SKEY_FUNCTION	; n=68, FUNCTION
	retlw	SKEY_W		; n=69, W
	retlw	SKEY_R		; n=70, R
	retlw	SKEY_Y		; n=71, Y
	retlw	SKEY_I		; n=72, I
	retlw	SKEY_P		; n=73, P
	retlw	SKEY_PARENRIGHT	; n=74, PARENRIGHT
	retlw	SKEY_PAGE	; n=75, PAGE
	retlw	NO_KEY		; n=76, no key
	retlw	NO_KEY		; n=77, no key
	retlw	NO_KEY		; n=78, no key
	retlw	SKEY_TAB	; n=79, TAB
	retlw	SKEY_Q		; n=80, Q
	retlw	SKEY_E		; n=81, E
	retlw	SKEY_T		; n=82, T
	retlw	SKEY_U		; n=83, U
	retlw	SKEY_O		; n=84, O
	retlw	SKEY_PARENLEFT	; n=85, PARENLEFT
	retlw	SKEY_BACKSPACE	; n=86, BACKSPACE
	retlw	NO_KEY		; n=87, no key
	retlw	NO_KEY		; n=88, no key
	retlw	NO_KEY		; n=89, no key
	retlw	SKEY_COLON	; n=90, COLON
	retlw	SKEY_TWO	; n=91, 2
	retlw	SKEY_FOUR	; n=92, 4
	retlw	SKEY_SIX	; n=93, 6
	retlw	SKEY_EIGHT	; n=94, 8
	retlw	SKEY_ZERO	; n=95, 0
	retlw	SKEY_EQUAL	; n=96, EQUAL
	retlw	SKEY_BACKSLASH	; n=97, BACKSLASH
	retlw	NO_KEY		; n=98, no key
	retlw	NO_KEY		; n=99, no key
	retlw	NO_KEY		; n=100, no key
	retlw	SKEY_ONE	; n=101, 1
	retlw	SKEY_THREE	; n=102, 3
	retlw	SKEY_FIVE	; n=103, 5
	retlw	SKEY_SEVEN	; n=104, 7
	retlw	SKEY_NINE	; n=105, 9
	retlw	SKEY_MINUS	; n=106, MINUS
	retlw	SKEY_GRAVE	; n=107, GRAVE
	retlw	SKEY_BAR	; n=108, BAR
	retlw	NO_KEY		; n=109, no key
	retlw	NO_KEY		; n=110, no key
	retlw	NO_KEY		; n=111, no key
	retlw	SKEY_ESCAPE	; n=112, ESCAPE
	retlw	SKEY_REFRESH	; n=113, REFRESH
	retlw	SKEY_SQUARE	; n=114, SQUARE
	retlw	SKEY_CIRCLE	; n=115, CIRCLE
	retlw	SKEY_TRIANGLE	; n=116, TRIANGLE
	retlw	SKEY_CLEAR_INPUT; n=117, CLEAR_INPUT
	retlw	SKEY_SUSPEND	; n=118, SUSPEND
	retlw	SKEY_RESUME	; n=119, RESUME
	retlw	NO_KEY		; n=120, no key
	retlw	NO_KEY		; n=121, no key
	retlw	NO_KEY		; n=122, no key
	retlw	NO_KEY		; n=123, no key
	retlw	NO_KEY		; n=124, no key
	retlw	NO_KEY		; n=125, no key
	retlw	NO_KEY		; n=126, no key
	retlw	NO_KEY		; n=127, no key
end_sym_key_decode
	no_page_cross	decode_sym_key, end_sym_key_decode


    global	sym_kbd_init
    global	sym_kbd_test

END